﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
//using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace COC_helper
{
    public partial class SessionLeader : Form
    {
        /// <summary>
        /// 当前选择是否为人物,否则为地图选项
        /// </summary>
        private bool _isPeo = true;
        public List<Label> _labels = new List<Label>();
        public SessionLeader()
        {
            InitializeComponent();
        }

        private void Button_Map_Click(object sender, EventArgs e)
        {
            _isPeo = false;
            Button_Map.BackColor = Color.Green;
            Button_Peo.BackColor = Color.White;
            ListBox_All.Items.Clear();
            if (Global_Value.zmb_Setting.MapHandles.Count == 0)
            { MessageBox.Show("警告,地图列表是空的");
                Global_Value.MyLogHandle.LogPush("会话编辑器:" + "警告,地图列表是空的", "Wran");
            }
            foreach(var item in Global_Value.zmb_Setting.MapHandles)
                ListBox_All.Items.Add(item.Label);
            

        }

        private void Button_Peo_Click(object sender, EventArgs e)
        {
            _isPeo = true;
            Button_Peo.BackColor= Color.Green;
            Button_Map.BackColor= Color.White;
            ListBox_All.Items.Clear();
            if (Global_Value.zmb_Setting.PeopleList.Count == 0)
            { MessageBox.Show("人物列表是空的,请先去添加人物");
                Global_Value.MyLogHandle.LogPush("会话编辑器:" + "警告,人物列表是空的", "Wran");
            }
            foreach (var item in Global_Value.zmb_Setting.peoEdites)
                ListBox_All.Items.Add(item.Name);
        }

        private void ListBox_All_SelectedValueChanged(object sender, EventArgs e)
        {
            if (ListBox_All.SelectedItem == null)
                return;
        }
        bool Moving = false;                // 标识是否在拖动控件
        Point MouseFirstLocation;           // 鼠标按下时, 相对于控件的坐标

        private void ExempleButton_MouseDown(object sender, MouseEventArgs e)
        {
            Label mysender = (Label)sender;
            mysender.BackColor = Color.Blue;
            Moving = true;                                                   // 表示进入拖动控件的状态
            Point MousePoint = Control.MousePosition;                        // 获取鼠标相对屏幕的坐标
            MouseFirstLocation = mysender.PointToClient(MousePoint);    // 获取坐标相对于控件的相对坐标并赋值给MouseFirstLocation
        }

        private void ExempleButton_MouseMove(object sender, MouseEventArgs e)
        {
            Label mysender = (Label)sender;
            if (Moving)
            {
                Point MousePoint = Control.MousePosition;                                                                                                   // 获取鼠标相对屏幕的坐标
                Point MousePointToContainer = mysender.Parent.PointToClient(MousePoint);                                                               // 获取鼠标相对控件父容器的坐标
                Point ControlNewLocation = new Point(MousePointToContainer.X - MouseFirstLocation.X, MousePointToContainer.Y - MouseFirstLocation.Y);       // 计算控件应处于的, 新的坐标
                mysender.Location = ControlNewLocation;                                                                                                // 移动控件
            }
        }

        private void ExempleButton_MouseUp(object sender, MouseEventArgs e)
        {
            Label mysender = (Label)sender;
            mysender.BackColor = Color.White;
            Moving = false;                                                  // 脱离拖动控件的状态
            
        }


        private void ListBox_All_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (ListBox_All.SelectedItem == null)
                    return;
                if (_isPeo) // 添加的是人物
                {
                    if (ListBox_Peo.Items.Contains(ListBox_All.SelectedItem.ToString()))
                        throw new Exception("要添加的对象" + ListBox_All.SelectedItem.ToString() + "已经存在了");
                    ListBox_Peo.Items.Add(ListBox_All.SelectedItem.ToString());
                    Label label = new Label();
                    label.Text = ListBox_All.SelectedItem.ToString();
                    label.BackColor = Color.White;
                    label.Name = "Label_Name" + (_labels.Count + 1).ToString();
                    label.AutoSize = true;
                    label.Location = new Point(10, 10 + _labels.Count * 20);
                    label.Visible = true;
                    label.Font = new Font("宋体", 12);
                    label.ForeColor = Color.Black;
                    label.MouseDown += ExempleButton_MouseDown;
                    label.MouseUp += ExempleButton_MouseUp;
                    label.MouseMove += ExempleButton_MouseMove;
                    //label.Parent = panel1;

                    //pictureBox1.Refresh();
                    label.TabIndex = 100 + _labels.Count;
                    label.BringToFront();

                    _labels.Add(label);
                    this.Controls.Add(label);
                    pictureBox1.SendToBack();
                    label.Show();
                    //this.Refresh();

                }
                else //添加的是地图
                {
                    var item = Global_Value.zmb_Setting.MapHandles.Find(one => one.Label == ListBox_All.SelectedItem.ToString());
                    //if (Image.FromFile(Global_Value.ZMB_PATH + item.Path) == null)
                    //    throw new Exception("该地图的文件丢失或者被占用,无法打开");
                    TextBox_Map.Text = item.Label;
                    item.User = TextBox_Name.Text;
                    pictureBox1.Image =System.Drawing.Image.FromFile(Global_Value.ZMB_PATH + item.Path);



                }
            }
            catch (Exception err)
            {
                Global_Value.MyLogHandle.LogPush("会话编辑器:"+ err.ToString(), "Wran");
                MessageBox.Show(err.ToString());
            }
            
        }

        private void SessionLeader_Shown(object sender, EventArgs e)
        {
            try
            {
                if (Global_Value.zmb_Setting.SessionHandles == null)
                    throw new Exception("会话列表是空的");
                foreach (var item in Global_Value.zmb_Setting.SessionHandles)
                {
                    ListBox_Session.Items.Add(item.Name);

                }
                ListBox_Session.SelectedIndex = 0;
            }
            catch (Exception err)
            {
                Global_Value.MyLogHandle.LogPush("会话编辑器:" + err.ToString(), "Wran");
                //throw;
            }
        }

        private void 删除选中的对象ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Global_Value.MyLogHandle.LogPush("会话编辑器:" + "准备操作类 MapHandles", "Info");
            if (ListBox_Peo.SelectedItem == null)
                return;
            Global_Value.zmb_Setting.MapHandles.Remove(Global_Value.zmb_Setting.MapHandles.Find(one => one.Label == ListBox_Peo.SelectedItem.ToString()));

            //var controls = this.GetAllControls()
            //                  .Where(ctrl => ctrl.Text == text)
            //                  .ToList();
            Control temp = new Control();
            foreach (var item in _labels)
            {
                if(item.Text == ListBox_Peo.SelectedItem.ToString())
                {
                    temp = (this.Controls.Find(item.Name.ToString(), false))[0];
                    _labels.Remove(item);
                    break;
                    //MessageBox.Show(temp.ToString());
                }
            }
            
            this.Controls.Remove(temp);

            ListBox_Peo.Items.Remove(ListBox_Peo.SelectedItem);

        }

        private void Button_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox_Name == null)
                    throw new Exception("名字不能为空");
                if (TextBox_Map.Text == null)
                    throw new Exception("需要为其指定一张地图");
                SessionHandle temp = Global_Value.zmb_Setting.SessionHandles.Find(one => one.Name == TextBox_Name.Text);
                if (temp != null)
                {
                    if (MessageBox.Show("这个名字的会话已经有了,要覆盖吗?", "重复的名字", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }
                    Global_Value.zmb_Setting.SessionHandles.Remove(temp);
                    ListBox_Session.Items.Remove(ListBox_Session.SelectedItem);
                }
                else
                { 
                    temp = new SessionHandle();
                }
                PeoEdite peoEdite = new PeoEdite();
                temp.PeoEdites.Clear();
                foreach (var item in ListBox_Peo.Items)
                {
                    peoEdite = Global_Value.zmb_Setting.peoEdites.Find(one => one.Name == item.ToString());
                    peoEdite.Add_X = _labels.Find(one => one.Text == item.ToString()).Location.X;
                    peoEdite.Add_Y = _labels.Find(one => one.Text == item.ToString()).Location.Y;
                    peoEdite.Tale = TextBox_Name.Text;
                    peoEdite.Place = TextBox_Map.Text;
                    peoEdite.State = "正在出场";
                    temp.PeoEdites.Add(peoEdite);
                }
                MapHandle mapHandle = Global_Value.zmb_Setting.MapHandles.Find(one => one.Label == TextBox_Map.Text);
                if (mapHandle == null) throw new Exception("地图存储库错误");
                //temp.MapHandle.Path = mapHandle.Path;
                temp.Name = TextBox_Name.Text;
                //temp.MapHandle.Label = 
                temp.MapHandle = mapHandle;
                ListBox_Session.Items.Add(temp.Name);
                Global_Value.zmb_Setting.SessionHandles.Add(temp);


            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
                Global_Value.MyLogHandle.LogPush("会话编辑器:" + err.ToString(), "Wran");

            }
           
        }

        private void ListBox_Session_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ListBox_Session.SelectedItem == null)
                { return; }

                SessionHandle sessionHandle = Global_Value.zmb_Setting.SessionHandles.Find(one => one.Name == ListBox_Session.Text);
                TextBox_Name.Text = sessionHandle.Name;
                ListBox_Peo.Items.Clear();
                pictureBox1.Image = System.Drawing.Image.FromFile(Global_Value.ZMB_PATH + sessionHandle.MapHandle.Path);
                TextBox_Map.Text = sessionHandle.MapHandle.Label;
                foreach (var item in _labels)
                {
                    this.Controls.Remove(item);
                }
                _labels.Clear();
                foreach (var item in sessionHandle.PeoEdites)
                {
                    ListBox_Peo.Items.Add(item.Name);
                    Label label = new Label();
                    label.Text = item.Name;
                    label.BackColor = Color.White;
                    label.Name = "Label_Name" + (_labels.Count + 1).ToString();
                    label.AutoSize = true;
                    label.Location = new Point(item.Add_X, item.Add_Y);
                    label.Visible = true;
                    label.Font = new Font("宋体", 12);
                    label.ForeColor = Color.Black;
                    label.MouseDown += ExempleButton_MouseDown;
                    label.MouseUp += ExempleButton_MouseUp;
                    label.MouseMove += ExempleButton_MouseMove;
                    //label.Parent = panel1;

                    //pictureBox1.Refresh();
                    label.TabIndex = 100 + _labels.Count;
                    label.BringToFront();

                    _labels.Add(label);
                    this.Controls.Add(label);
                    pictureBox1.SendToBack();
                    label.Show();

                }
            }
            catch (Exception err)
            {
                Global_Value.MyLogHandle.LogPush("会话编辑器:" + err.ToString(), "Wran");
                MessageBox.Show(err.Message.ToString());
            }
            
        }

        private void 将这个保存为历史数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ListBox_Session.SelectedItems == null) return;

        }

        private void 删除选中的会话ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (ListBox_Session.SelectedItems == null) return;
                SessionHandle sessionHandle = Global_Value.zmb_Setting.SessionHandles.Find(one => one.Name == ListBox_Session.Text) ?? throw new Exception("该会话不存在了,刷新可解决");
                PeoEdite peoEdite = new PeoEdite();
                foreach (var item in sessionHandle.PeoEdites)
                {
                    peoEdite = Global_Value.zmb_Setting.peoEdites.Find(one => one.Name == item.Name.ToString());
                    if (peoEdite == null) throw new Exception("列表中找不到这样的人物");
                    peoEdite.Add_X = 0;
                    peoEdite.Add_Y = 0;
                    peoEdite.Tale = "";
                    peoEdite.State = "未出场";
                    peoEdite.Place = "未出场";
                }
                var item2 = Global_Value.zmb_Setting.MapHandles.Find(one => one.Label == ListBox_All.SelectedItem.ToString());
                item2.User = "无";
                Global_Value.zmb_Setting.SessionHandles.Remove(sessionHandle);
                ListBox_Session.Items.Remove(ListBox_Session.SelectedItem);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message.ToString());
                Global_Value.MyLogHandle.LogPush("会话编辑器:" + err.ToString(), "Wran");
                //throw;
            }
           

            //_labels.Clear();

        }

        private void SessionLeader_Load(object sender, EventArgs e)
        {

        }

        private void ListBox_Peo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(ListBox_Peo.SelectedItem == null) return;
            Control temp = new Control();
            foreach (var item in _labels)
            {
                temp = (this.Controls.Find(item.Name.ToString(), false))[0];
                if (item.Text == ListBox_Peo.SelectedItem.ToString())
                    temp.BackColor = Color.Green;
                else
                    temp.BackColor = Color.White;

            }

        }
    }
}
